/*
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#define _GNU_SOURCE
#include <string.h>
#include <stdlib.h>
#include <libgen.h>
#include "album.h"
#include "feature.h"
#include "list.h"
#include "debug.h"

/**
 * @brief  free a album
 */
void album_free(Album *A) {
    struct list_head *ptr, *n;
    list_del(&A->node);
    list_for_each_safe(ptr, n, &A->songs_head) {
        list_del_init(ptr);
    }
    free(A->name);
    free(A->dir);
    free(A->image);
    free(A);
}


/**
 * @brief Asking the configure which album construct type is set default is Directory
 * @return the construct type
 */
enum ALBUM_TYPE album_construct_type_get() {
    const char * KEYWORD = "ALBUMTYPE";
    Feature *F = mokoko_feature_search(NULL, KEYWORD);
    char const *str;
    if (!F)
        return DIRECTORY;
    str = mokoko_feature_value_get(NULL, KEYWORD, 0);
    if (!str)
        return DIRECTORY;
    if (!strcmp(str, "ID3ALBUM"))
        return ID3ALBUM;
    if (!strcmp(str, "ID3AUTHOR"))
        return ID3AUTHOR;
    if (!strcmp(str, "ID3YEAR"))
        return ID3YEAR;
    if (!strcmp(str, "FILESIZE"))
        return FILESIZE;
    if (!strcmp(str, "FILEDATE"))
        return FILEDATE;
    return DIRECTORY;
}


/**
 * @brief Creating a new album
 * @param name the name of this album
 * @param dir the dir of this album (only used with Directory construct type)
 * @return the New Album, NULL if err
 */
Album *album_new(const char *name, const char *dir) {
    Album * album = NULL;
    if (!name)
        return NULL;
    album = (Album *)calloc(1, sizeof(Album));
    album->name = strdup(name);
    if (dir)
        album->dir = strdup(dir);
    INIT_LIST_HEAD(&album->node);
    INIT_LIST_HEAD(&album->songs_head);
    debug("Creating Album %s\n", name);
    return album;
}


/**
 * @brief Adding a song to an album
 * @param A the Album
 * @param S the Song going to be added
 */
void album_song_add(Album *A, Song *S) {
    if (!A || !S) {
        debug("Bad paramters\n");
        return;
    }
    debug("Adding Song %s to Album %s\n", basename(strdupa(S->file)),  A->name);
    list_add(&S->album_node, &A->songs_head);
    A->count++;
}


/**
 * @brief remove a song from an album
 * @param A the Album
 * @param S the song is going to remove
 * @return 0 if successful, else if error happens
 */ 
int album_song_del(Album *A, Song *S) {
    Song *S_ptr, *Sn;
    if (!A || !S) {
        debug("Bad paramters\n");
        return -1;
    }
    list_for_each_entry_safe(S_ptr, Sn, &A->songs_head, album_node) {
        if (S_ptr == S) {
            list_del_init (&S->album_node);
            A->count--;
            return 0;
        }
    }
    debug("Song is not found\n");
    return -1;
}
